*************************************** Replication Code for The Impact of Automation and Artificial Intelligence on Worker Well-Being - Nazareno & Schiff ***************************************

* This file includes code to replicate the main analyses described in: 
* Nazareno, Luisa, and Daniel Schiff. 2021. The Impact of Automation and Artificial Intelligence on Worker Well-Being.� Technology in Society. https://doi.org/10.1016/j.techsoc.2021.101679.
* For further information, contact the corresponding author at lnazareno@gsu.edu

* Note: Lines 17 - 169 provide the code applied to the raw GSS dataset (available at https://gss.norc.org/get-the-data/stata) in order to extract the main dataset used for analysis, ("GSS.dta")
* We merge these files with ALM ("ALM_dot_8090.xls") and FO ("Frey Osborne Computerisable Table.xls"), which are provided in Excel.

	
* Generating GSS sample frow raw dataset ("GSS7218_R1.dta")
	* Note that in GSS: 
		//.i: Inapplicable (IAP). Respondents who are not asked to answer a specific question are assigned to IAP.
		//.d: Don't know (DK)
		//.n: No answer (NA)

	cd "C:\Users\luisa\Documents\papers\Automation and well-being\Submission_Technology in Society\replication files" //change directory as needed
	* Selecting variables from GSS
	#delimit ;
	use
	 year id cohort region dateintv autonojb 
	 wrkstat hrs1 hrs2 occ occ80 occonet occstatus occtag coocc10 occ10 indus07 indus10 conrinc income rincome realinc
	 age educ childs degree sex  race marital hispanic joblose
	 satjob1 joblose autonojb health1 happy unhappy health mntlhlth physhlth stress hurtatwk  born 
	 usetech lifenow wtssall wrktype yearsjob waypaid wrksched moredays mustwork chngtme
	 wrkhome whywkhme famwkoff wkvsfam famvswk hrsrelax secondwk learnnew
	 workfast workdiff lotofsay wktopsat overwork knowwhat myskills respect
	 trustman safetywk safefrst teamsafe safehlth proudemp prodctiv wksmooth
	 trdunion partteam wkdecide setthngs toofewwk promteok opdevel hlpequip
	 haveinfo wkfreedm fringeok supcares condemnd promtefr cowrkint jobsecok
	 suphelp wrktime cowrkhlp trainops manvsemp hvylift handmove wkpraise
	 wkbonus fairearn rincblls laidoff jobfind1 trynewjb wkageism wkracism
	 wksexism wkharsex wkharoth health1 physhlth mntlhlth hlthdays usedup
	 backpain painarms hurtatwk spvtrfair suprvsjb strredpg phyeffrt slpprblm
	 knowschd usetech stress12 hyperten arthrtis diabetes depress weight
	 height ntwkhard misswork lifenow lifein5 
	using "GSS7218_R1.dta", clear
	 ;
	#delimit cr

	keep if year==2002 | year==2006 | year==2010 | year==2014 | year==2018 
	keep if wrkstat==1 | wrkstat==2 //full-time and part-time workers
	drop if occ80==.n //drop missing occupation codes in occ80
	drop if occ10==.n //drop missing occupation codes in occ10
	

* Generating control variables
	* Gender
	gen female=sex==2
	
	* Race / ethnicity
	gen hispanicdummy=1
	replace hispanicdummy=0 if hispanic==1

	gen raceh=1 if race==1 //white
	replace raceh=2 if race==2 //black
	replace raceh=4 if race==3
	replace raceh=3 if hispanicdummy==1
	tab raceh, m
	label define raceh 1 "White" 2 "Black" 3 "Hispanic" 4 "Other"
	label val raceh raceh

	* Education
	drop if degree>4 //drop missing degree

	* Age
	gen age2=age^2
	drop if age>88 //dropped 1 "89 and older" and 25 missing

	* Marital status
	gen married=marital==1
	tab marital, m nol
	drop if marital > 5 //drop missing

	* Region
	gen region4=1 if region==1 | region==2 //northeast = new england + mid-atlantic
	replace region4=2 if  region==3 | region==4 //midwest = east north central + west north central
	replace region4=3 if  region==5 | region==6 | region==7 //south = south atlantic + east south central + west south central
	replace region4=4 if  region==8 | region==9 //west = mountain + pacific
	tab region region4, m
	gen northeast=region==1
	gen midwest=region==2
	gen south=region==3
	gen west=region==4

	* Industry - *Aggregating industry (Following ACS technical documentation)
	//GSS indus10 corresponds to 2007 Census Code
	gen industry=1 if 170<=indus10 & indus10<=290  //Agriculture, Forestry, Fishing, and Hunting, and Mining
	replace industry=1 if 370<=indus10 & indus10<=490
	replace industry=2 if indus10==770 //Construction 
	replace industry=3 if 1070<=indus10 & indus10<=3990 //Manufacturing
	replace industry=4 if 4070<=indus10 & indus10<=4590 //Wholesale and Retail Trade
	replace industry=4 if 4670<=indus10 & indus10<=5790
	replace industry=5 if 6070<=indus10 & indus10<=6390  //Transportation and Warehousing and Utilities
	replace industry=5 if 570<=indus10 & indus10<=690 
	replace industry=6 if 6470<=indus10 & indus10<=6780 //Information
	replace industry=7 if 6870<=indus10 & indus10<=7190 //Finance and Insurance, and Real Estate, and Rental and Leasing
	replace industry=8 if 7270<=indus10 & indus10<=7790 //Professional, Scientific, and Management, and Administrative, and Waste Management Services
	replace industry=9 if 7860<=indus10 & indus10<=8470 //Educational Services, and Health Care and Social Assistance
	replace industry=10 if 8560<=indus10 & indus10<=8690 //Arts, Entertainment, and Recreation, and Accommodation and Food Services
	replace industry=11 if 8770<=indus10 & indus10<=9290 //Other Services, Except Public Administration
	replace industry=12 if 9370<=indus10 & indus10<=9590 // Public Administration

	tab indus10 if industry==.  //9 missing = armed forces
	drop if industry==.

	* Part-time
	gen parttime=wrkstat==2


* Variables for factor analysis
	* Order all variables from less to more (same direction)
	gen safetywk2 =5-safetywk 
	gen safefrst2=5-safefrst
	gen teamsafe2=5-teamsafe
	gen safehlth2=5-safehlth
	gen supcares2=5-supcares
	gen promtefr2=5-promtefr
	gen cowrkint2=5-cowrkint
	gen manvsemp2=6-manvsemp  
	gen wkpraise2=4-wkpraise
	gen trustman2=5-trustman
	gen respect2=5-respect
	gen cowrkhlp2=5-cowrkhlp
	gen wksmooth2=5-wksmooth
	gen toofewwk2=5-toofewwk
	tab hlpequip //keep as is
	tab haveinfo //keep as is
	gen knowwhat2=5-knowwhat 
	gen wkdecide2=5-wkdecide
	gen wkfreedm2=5-wkfreedm
	gen opdevel2=5-opdevel
	tab wrktime //keep as is
	gen overwork2=5-overwork
	gen prodctiv2=5-prodctiv
	gen learnnew2=5-learnnew
	gen workfast2=5-workfast
	gen workdiff2=5-workdiff
	gen myskills2=5-myskills
	gen hvylift2=hvylift==2
	gen handmove2=handmove==2
	gen promteok2=5-promteok
	gen fringeok2=5-fringeok
	

* Sample definition
	mark nomiss 
	markout nomiss year age female raceh married degree industry
	
	mark nomisswork
	markout nomisswork safetywk safefrst teamsafe safehlth supcares promtefr cowrkint manvsemp wkpraise ///
	trustman respect cowrkhlp wksmooth toofewwk hlpequip haveinfo knowwhat wkdecide  ///
	wkfreedm opdevel wrktime overwork prodctiv learnnew workfast workdiff myskills ///
	hvylift handmove promteok fringeok
	keep if nomisswork==1
	save "GSS.dta", replace


* Merging with Autor, Levy and Murnane 2003
	* Using Census 1980 occ. occ80 // info available for 8803/14194
	* Note that OCC in GSS refers to 1970 census occ code
	* The skills in ALM are not mutually exclusive. In an excel file ("ALM_dot_8090.xls"), we identify the percentage of tasks that are routine in each occupation
	//by dividing its score by the sum of all scores: totalroutinescore / (sum all scores: math, dcp, sts, finger and efh)
	import excel "ALM_dot_8090.xls", sheet("data") firstrow clear
	rename occ8090 occ80
	merge 1:m occ80 using "GSS.dta"
	drop if _merge==1
	drop _merge
	save "GSS.dta", replace
		

* Merging with Frey and Osborne
	* Translating FO SOC codes into COC codes (both 2010) using Census' crosswalk table ("Frey Osborne Computerisable Table.xls")
	import excel "Frey Osborne Computerisable Table.xls", sheet("Data") firstrow clear
	
	gen pcomputerization = FO_prob
	replace pcomputerization =  FO_adj_prob if FO_prob==. &  FO_adj_prob<.
	
	drop if pcomputerization==.
	
	destring CENSUScode, replace
	gen occ10 = CENSUScode
	drop if occ10==.
	keep SOCcode occ10 CENSUSname SOCname pcomputerization
	format %20s CENSUSname
	format %20s SOCname
	
	merge 1:m occ10 using "GSS.dta"
	drop if _merge==1
	
	
	* Reorganizing
	rename id identifier
	order occ80 occ10 pcomputerization ALM_rout_pc ALM_routcog_pc ALM_routman_pc satjob1 stress health1 physhlth mntlhlth
	rename CENSUSname CENSUS10name
	rename SOCname SOC10name
	

	* Eliminating records with missing probabilities in FO AND ALM
	drop if pcomputerization==. & ALM_rout_pc==. //126
	drop if pcomputerization==.


* Independent variables  
	tab satjob1 , nol //1-4 scale from VERY to NOT AT ALL (job satisfaction in general)
	tab stress , nol //1-5 scale from ALWAYS to NEVER (how often R find wok stressful)
	tab health1 , nol //1-5 scale from EXCELLENT to POOR (r's health in general)
	tab jobsecok , nol //1-4 scale from VERY TRUE to NOT AT ALL TRUE (the job security is good)

	* Order all variables from less to more (same direction)
	gen jsats = 5-satjob1 //1=not at all satisfied, 4=very satisfied
	gen jstress = 6-stress //1=never stressed, 5=always stressed
	gen healthy = 6-health1 //1=poor, 5=excellent
	gen jobsecurity = 5-jobsecok //1=job is not secure at all to 4=job is very secure

	drop if jsats==. & jstress==. & healthy==. &  jobsecurity==. //sample has 7811 obs (447 deleted)
	drop if jsat==. | jstress==. | healthy==.


* Generating factor variables
	factor safetywk2 safefrst2 teamsafe2 safehlth2 supcares2 promtefr2 cowrkint2 manvsemp2 wkpraise2 ///
	trustman2 respect2 cowrkhlp2 wksmooth2 toofewwk2 hlpequip haveinfo knowwhat2 wkdecide2  ///
	wkfreedm2 opdevel2 wrktime overwork2 prodctiv2 learnnew2 workfast2 workdiff2 myskills2 ///
	hvylift2 handmove2 promteok2 fringeok2, pcf
	rotate, promax (2)
	predict Fp21 Fp22 Fp23 Fp24 Fp25 Fp26
	sortl //Table A1
	
	gen wkconditions2 = Fp21
	gen social2 = Fp22
	gen autonomy2 = Fp23
	gen workload2 = Fp24
	gen benefits2 = Fp25
	gen manual2 = Fp26

	* Calculating alpha
	alpha wksmooth2 wkpraise2 respect2 trustman2 supcares2 promtefr2 manvsemp2 cowrkint2 cowrkhlp2, std
	alpha teamsafe2 safetywk2 safehlth2 safefrst2 prodctiv2, std
	alpha wkfreedm2 workdiff2 wkdecide2 opdevel2 myskills2 learnnew2 knowwhat2 , std
	alpha wrktime workfast2 toofewwk2 overwork2 hlpequip haveinfo , std
	alpha promteok2 fringeok2, std
	alpha hvylift2 handmove2, std


* New vars
	egen pcompstd = std(pcomputerization), mean(0) std(1) //standardizing probability of computerisation

	gen white=raceh==1
	gen black=raceh==2
	gen hisp=raceh==3
	gen other=raceh==4

	gen lesshs=degree==0
	gen hs=degree==1
	gen junior=degree==2
	gen bachelor=degree==3
	gen grad=degree==4

	gen ind1=industry==1
	gen ind2=industry==2
	gen ind3=industry==3
	gen ind4=industry==4
	gen ind5=industry==5
	gen ind6=industry==6
	gen ind7=industry==7
	gen ind8=industry==8
	gen ind9=industry==9
	gen ind10=industry==10
	gen ind11=industry==11
	gen ind12=industry==12

	gen year02=year==2002
	gen year06=year==2006
	gen year10=year==2010
	gen year14=year==2014
	gen year18=year==2018


* Analysis
* Table 1
	sum jsat jstress healthy jobsecurity pcomputerization pcompstd year* age female ///
	white black hisp other married parttime lesshs hs junior bachelor grad ind* wkconditions2 social2 benefits2 manual2 workload2 autonomy2 if pcomputerization<.

	
* Table 2 - OLS regressions 
	* Satisfaction
	reg jsats pcompstd 
	outreg2 using "table2.xls", dec(2) beta  replace
	reg jsats pcompstd i.year age age2 female i.raceh married i.degree parttime i.industry 
	outreg2 using "table2.xls", dec(2) beta 
	reg jsats pcompstd i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "table2.xls", dec(2) beta 

	* Stress
	reg jstress pcompstd 
	outreg2 using "table2.xls", dec(2) beta
	reg jstress pcompstd i.year age age2 female i.raceh married i.degree parttime i.industry 
	outreg2 using "table2.xls", dec(2) beta 
	reg jstress pcompstd i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "table2.xls", dec(2) beta

	* Health	
	reg healthy pcompstd 
	outreg2 using "table2.xls", dec(2) beta
	reg healthy pcompstd i.year age age2 female i.raceh married i.degree parttime i.industry 
	outreg2 using "table2.xls", dec(2) beta
	reg healthy pcompstd i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "table2.xls", dec(2) beta


* Alternative specifications (Figure 2)
	* Dummies for high-risk versus low-risk
	gen p7525=1 if pcomputerization>=.75
	replace p7525=0 if pcomputerization<=.25
	tab p7525, m // N: high-risk=2189, low-risk=2,111
	tab occ10 if p7525==1, m //171 occ
	tab occ10 if p7525==0, m //127 occ
	 
	gen p9505=1 if pcomputerization>=.95
	replace p9505=0 if pcomputerization<=.05
	tab p9505, m // N: high risk=1,105, low risk=517
	tab occ10 if p9505==1, m //40 occ
	tab occ10 if p9505==0, m //70 occ
	 
	 
	* Regressions
	* Continuous
	reg jsats pcompstd i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "fig2.xls", dec(4) beta replace
	reg jstress pcompstd i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "fig2.xls", dec(4) beta
	reg healthy pcompstd i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "fig2.xls", dec(4) beta
	
	* p7525 (75th and 25th percentiles)
	reg jsats p7525 i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "fig2.xls", dec(4) beta
	reg jstress p7525 i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "fig2.xls", dec(4) beta
	reg healthy p7525 i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "fig2.xls", dec(4) beta

	* p9505 (95th and 5th percentiles)
	reg jsats p9505 i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "fig2.xls", dec(4) beta
	reg jstress p9505 i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "fig2.xls", dec(4) beta
	reg healthy p9505 i.year age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "fig2.xls", dec(4) beta

	
* Table 3 - DID regressions
	* Gen vars for interactions
	tab year
	gen y02=year==2002
	gen y06=year==2006
	gen y10=year==2010
	gen y14=year==2014
	gen y18=year==2018

	gen pcomp06=pcompstd*y06
	gen pcomp10=pcompstd*y10
	gen pcomp14=pcompstd*y14
	gen pcomp18=pcompstd*y18
		
	* Regressions
	reg jsats pcompstd 
	outreg2 using "table3.xls", dec(2) beta replace
	reg jsats pcompstd y06 y10 y14 y18 pcomp06 pcomp10 pcomp14 pcomp18 age age2 female i.raceh married i.degree parttime i.industry 
	outreg2 using "table3.xls", dec(2) beta
	reg jsats pcompstd y06 y10 y14 y18 pcomp06 pcomp10 pcomp14 pcomp18  age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "table3.xls", dec(2) beta

	reg jstress pcompstd 
	outreg2 using "table3.xls", dec(2)  beta
	reg jstress pcompstd y06 y10 y14 y18 pcomp06 pcomp10 pcomp14 pcomp18 age age2 female i.raceh married i.degree parttime i.industry 
	outreg2 using "table3.xls", dec(2) beta
	reg jstress pcompstd y06 y10 y14 y18 pcomp06 pcomp10 pcomp14 pcomp18  age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2  workload2 autonomy2 
	outreg2 using "table3.xls", dec(2) beta

	reg healthy pcompstd 
	outreg2 using "table3.xls", dec(2)  beta
	reg healthy pcompstd y06 y10 y14 y18 pcomp06 pcomp10 pcomp14 pcomp18 age age2 female i.raceh married i.degree parttime i.industry 
	outreg2 using "table3.xls", dec(2) beta
	reg healthy pcompstd y06 y10 y14 y18 pcomp06 pcomp10 pcomp14 pcomp18  age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "table3.xls", dec(2) beta
		

* Table 4 - Insecurity
	reg jobsecurity pcompstd 
	outreg2 using "table4.xls", dec(2) beta replace
	reg jobsecurity pcompstd y06 y10 y14 y18 age age2 female i.raceh married i.degree parttime i.industry 
	outreg2 using "table4.xls", dec(2) beta
	reg jobsecurity pcompstd  y06 y10 y14 y18 age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 
	outreg2 using "table4.xls", dec(2) beta

	reg jsats pcompstd y06 y10 y14 y18 age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 jobsecurity
	outreg2 using "table4.xls", dec(2) beta
	reg jstress pcompstd y06 y10 y14 y18 age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 jobsecurity
	outreg2 using "table4.xls", dec(2) beta
	reg healthy pcompstd y06 y10 y14 y18 age age2 female i.raceh married i.degree parttime i.industry wkconditions2 social2 benefits2 manual2 workload2 autonomy2 jobsecurity
	outreg2 using "table4.xls", dec(2) beta

	
* Appendix D - ALM vs FO
	twoway (hist pcomputerization if ALM_rout_pc<., width(.05) freq fcolor(gs13) lcolor(gs12) xline(.25 .75, lcolor(gs5) lpattern(dash))) ///
    (hist ALM_rout_pc , freq width(.05) fcolor(none) lcolor( pink) legend(label(1 "FO (computerization prob.)") label(2 "ALM (% of routine task)")))
